package traverser; 
/* Try to traverse in the sequence of the frequence of links 
 * Author: Mark Daniel 
 * Date: Oct. 27th 
 *  
 * Not Working 
 */ 
import java.io.InputStream; 
import java.net.URL; 
import java.util.*; 

public class FreqFirstLastTraversal{ 
	String startPage,endPage; 
	LinkProcessor p; 

	String[] linkList; 
	String[] item;
	int[] counter; 
	String htmlCodes; 
	String[][] to_visit_matrix; 
	double[] freq;
	//DynamicMatrix matrixB=new DynamicMatrix();
	DynamicMatrix matrixA=new DynamicMatrix();


	public FreqFirstLastTraversal(String s){  
		startPage=s; 
		p=new LinkProcessor(startPage); 
		getHtml(startPage); 
		p.removeDuplicates(); 
		linkList=p.getList(); 
	} 

	public void getHtml(String website){ 
		URL site = null; 
		InputStream contentStream = null; 
		try { 
			site = new URL(website); 
			contentStream = site.openStream(); 
		} catch (Exception e) { 
			e.printStackTrace(); 
			System.exit(0); 
		} 

		Scanner sc = new Scanner(contentStream); 
		while (sc.hasNext()){ 
			htmlCodes += sc.nextLine(); 
		} 
	} 





	public void keywordSplit(){ 
		to_visit_matrix=new String[item.length][]; 
		for(int i=0;i<item.length;i++){ 
			String temp=item[i]; 
			to_visit_matrix[i]=temp.split("_"); 
			for(int j=0;j<to_visit_matrix[i].length;j++){				
				to_visit_matrix[i][j]=to_visit_matrix[i][j].toLowerCase();
				to_visit_matrix[i][j]=to_visit_matrix[i][j].replace("%27","'");
				to_visit_matrix[i][j]=to_visit_matrix[i][j].replace("(","");
				to_visit_matrix[i][j]=to_visit_matrix[i][j].replace(")","");
			}
		}
	} 


	public void getTheItem(){
		item=new String[linkList.length];
		for(int i=0;i<linkList.length;i++){ 
			item[i]=linkList[i].substring(29); 
		}        
	} 


	public void frequencyCount(){         
		getTheItem(); 
		keywordSplit(); 
		freq=new double[item.length];
		for(int i=0;i<freq.length;i++){
			int freqCounter=0;
			int length=0;
			for(int j=0;j<to_visit_matrix[i].length;j++){
				for(int k=0;k<to_visit_matrix.length;k++){
					for(int l=0;l<to_visit_matrix[k].length;l++){
						if(to_visit_matrix[i][j].equals(to_visit_matrix[k][l])) freqCounter++;
					}   		
				} 
			}
			freq[i]=freqCounter/to_visit_matrix[i].length;
		}
        parse();
        
        //printOutMatrixA();
		//printOutKeyWord();

		//printOut(); 
		//System.out.println("There are altogether "+linkList.length+" links.");        
	} 

	public void printOut(){        
		for(int i=0;i<linkList.length;i++) {System.out.println(linkList[i]+" "+counter[i]+" same links.");} 
	} 
	public void printOutKeyWord(){ 
		for(int i=0;i<to_visit_matrix.length;i++){ 
			for(int j=0;j<to_visit_matrix[i].length;j++){
				System.out.print(to_visit_matrix[i][j]+" ");

			} 
			System.out.println(" "+freq[i]+" times");
		} 

	} 
	public void printOutMatrixA(){ 
		for(int i=0;i<matrixA.sizeRow();i++){ 
			for(int j=0;j<matrixA.sizeColumn(i);j++){
				System.out.print(matrixA.get(i, j)+" ");

			} 
			System.out.println("");
		} 

	} 
	public void parse(){
		for(int i=0;i<linkList.length;i++){
			matrixA.set(i, 0, linkList[i]);
		}
		for(int i=0;i<to_visit_matrix.length;i++){
			for(int j=1;j<=to_visit_matrix[i].length;j++){
	
				matrixA.set(i, j, to_visit_matrix[i][j-1]);
			}			
		}
		for(int i=0;i<linkList.length;i++){
			String temp=freq[i]+"";
			matrixA.set(i,to_visit_matrix[i].length+1,temp);
		}
	}
	
	public DynamicMatrix returnDynamicMatrix(){
		return matrixA;
	}


	public static void main(String [] args){ 
		//Scanner s=new Scanner(System.in); 
		//System.out.print("Please input a website: "); 
		//String link=s.nextLine(); 
		String link="http://en.wikipedia.org/wiki/Apple_tree"; 
		FreqFirstLastTraversal f=new FreqFirstLastTraversal(link); 
		f.frequencyCount();         
	} 


	public void keywordMatch(String to_visit_matrix[][],String to_visit_matrix2[][]){ 


	} 


} 
